<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Derby Network Server sample program</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
</HEAD>
<BODY>
<H2><A name=Top_Of_Page></A><A name=nsrun></A><A name=nssample></A>Derby 
Network Server sample program</H2>
<P>The Network Server sample demo program (NsSample) is a simple JDBC application that 
interacts with the Derby Network Server. The program:</P>
<UL>
  <LI>starts the Derby Network Server 
  <LI>checks if the Derby Network Server is running 
  <LI>loads the DB2 JDBC Universal Driver or the Derby Client Network JDBC Driver
  <LI>creates the database '<tt>NsSampledb</tt>' if not already created 
  <LI>checks to see if the schema is already created, and if not, 
  creates the schema which includes the <tt>SAMPLETBL</tt> table and corresponding 
  indexes. 
  <LI>connects to the database 
  <LI>loads the schema by inserting data 
  <LI>starts client threads to perform database related operations 
  <LI>has each of the clients perform DML operations (select, insert, delete, update) 
  using JDBC calls, in particular one client opens an embedded connection to 
  perform database operations while the other client opens a client connection 
  to the Derby Network Server to perform database operations. 
  <LI>waits for the client threads to finish the tasks 
  <LI>shuts down the Derby Network Server at the end of the demo </LI></UL>
<P>The following files should be installed in the <SPAN 
class=italic>%DERBY_INSTALL%</SPAN><TT>\demo\programs\nserverdemo\</TT> directory in 
order to run the sample program:</P>
<UL>
  <LI><TT>NsSample.java</tt><br>
This is the entry point into the demo application. The program starts up two clients. One client opens an embedded 
connection to perform database operations while the other client opens a client 
connection to the Derby Network Server to perform database operations.  This program provides the following constants that 
can be changed to modify the sample demo:
<UL>

    <LI><tt>NUM_ROWS</tt> - The number of rows that must be initially loaded into the schema.</li>
    <LI><tt>ITERATIONS</tt> - The number of iterations for which each client thread does database related work.</LI>
<LI><tt>NUM_CLIENT_THREADS</tt> - The number of clients that you want to run the program against.</li> 
    <LI><tt>NETWORKSERVER_PORT</tt> - The port on which the network server is running.</LI>
</UL>

You can also modify the program to exclude starting the Network Server as part of the sample program. 
Make sure to recompile the java files before running the program if you make any 
changes to the source code.

</li>

<li><tt>NsSampleClientThread.java</tt></br>
This file contains two Java classes:
  <UL>
    <LI>The <tt>NsSampleClientThread</tt> class extends Thread and does all the 
necessary work by instantiating a <tt>NsSampleWork</tt> instance.</li>
    <LI>The <tt>NsSampleWork</tt> class contains everything required to perform 
DML operations using JDBC calls. The <tt>doWork</tt> method in 
the <tt>NsSampleWork</tt> class represents all the work done as 
part of this sample program.</LI>
</UL>
</li>
  <LI><tt>NetworkServerUtil.java</tt><br>
This file contains helper methods to start the Derby Network Server and to shutdown the server.
</li>
  <LI>Compiled class files:
<UL>
    <LI><tt>NsSample.class</tt></li> 
    <LI><tt>NsSampleClientThread.class</tt></li> 
    <LI><tt>NsSampleWork.class</tt></li> 
    <LI><tt>NetworkServerUtil.class</tt></li> 
</UL>
</li>
</ul>


<H2>Running the Network Server demo program</H2>
<P>To run the Derby Network Server demonstration program:</P>
<OL>
  <LI>Open a command prompt and change directories to the <SPAN 
  class=italic>%DERBY_INSTALL%</SPAN><tt>\demo\programs\</tt> directory, where <SPAN 
  class=italic>%DERBY_INSTALL%</SPAN> is the directory where you installed Derby. 
  <LI>Set the CLASSPATH to the current directory (".") and also include the following 
  jar files in order to use the Derby Network Server and the DB2 JDBC Universal Driver or the Derby Client Network JDBC Driver.
<ul>
<li><tt>derbynet.jar</tt><br>The Network Server jar file. It must be in your CLASSPATH to use any of the Derby Network 
Server functions.</li>
<li><tt>derbyclient.jar</tt><br>This jar file must be in your CLASSPATH to use the Derby Client Network JDBC Driver.</li>
<li><tt>db2jcc.jar</tt><br>This jar file must be in your CLASSPATH to use the 
DB2 JDBC Universal Driver.</li>
<li><tt>db2jcc_license_c.jar</tt><br>This jar file is the license file for the Universal 
Driver.</li>
<li><tt>derby.jar</tt><br>The Derby database engine jar file.</li>
</ul>
</LI>
<li>Test the CLASSPATH settings by running the following java command:<pre>java org.apache.derby.tools.sysinfo</pre>This command will show the Derby jar files that are in the CLASSPATH as well as 
the DB2 JDBC Universal Driver along with their respective versions.</li>
<li>Once you have set up your environment correctly, execute the application from the <br> <SPAN 
  class=italic>%DERBY_INSTALL%</SPAN><tt>\demo\programs\</tt> directory:<br>
<pre>java nserverdemo.NsSample [driverType]</pre>
<ul>
<li>Where the possible values for driverType are:
<ul>
<li>derbyClient (default)
<li>jccjdbcclient
</ul>
</ul>
</li>
</OL>
You should receive output similar to the following if the 
program runs successfully:<br><br>

<tt>
Using JDBC driver: org.apache.derby.jdbc.ClientDriver<br>
Derby Network Server created<br>
Server is ready to accept connections on port 1621.<br>
Connection number: 1.<br>
[NsSample] Derby Network Server started.<br>
[NsSample] Sample Derby Network Server program demo starting. <br>
Please wait .....................<br>
Connection number: 2.<br>
[NsSampleWork] Begin creating table - SAMPLETBL and necessary indexes. <br>
[NsSampleClientThread] Thread id - 1; started.<br>
[NsSampleWork] Thread id - 1; requests database connection, dbUrl =jdbc:derby:NSSampledb;<br>
[NsSampleClientThread] Thread id - 2; started.<br>
[NsSampleWork] Thread id - 2; requests database connection, dbUrl =jdbc:derby://localhost:1621/NSSampledb;create=true;<br>
Connection number: 3.<br>
[NsSampleWork] Thread id - 1 selected 1 row [998,Derby41        ,2.92903314E14,9572]<br>
[NsSampleWork] Thread id - 2 selected 1 row [998,Derby41        ,2.92903314E14,9572]<br>
[NsSampleWork] Thread id - 1 selected 1 row [998,Derby41        ,2.92903314E14,9572]<br>
[NsSampleWork] Thread id - 1 selected 1 row [998,Derby41        ,2.92903314E14,9572]<br>
[NsSampleWork] Thread id - 1 selected 1 row [998,Derby41        ,2.92903314E14,9572]<br>
[NsSampleWork] Thread id - 1; inserted 1 row.<br>
[NsSampleWork] Thread id - 1 selected 1 row [998,Derby41        ,2.92903314E14,9572]<br>
[NsSampleWork] Thread id - 1 selected 1 row [52,Derby41        ,2.92903314E14,9572]<br>
[NsSampleWork] Thread id - 1 selected 1 row [52,Derby41        ,2.92903314E14,9572]<br>
[NsSampleWork] Thread id - 2; updated 1 row with t_key = 9572<br>
[NsSampleWork] Thread id - 1; updated 1 row with t_key = 9572<br>
[NsSampleWork] Thread id - 2 selected 1 row [53,Derby41        ,2.92903314E14,9572]<br>
[NsSampleWork] Thread id - 1; inserted 1 row.<br>
[NsSampleWork] Thread id - 1; closed connection to the database.<br>
[NsSampleClientThread] Thread id - 1; finished all tasks.<br>
[NsSampleWork] Thread id - 2; deleted 1 row with t_key = 9572<br>
[NsSampleWork] Thread id - 2 selected 1 row [923,Derby14        ,4122.9033,9180]<br>
[NsSampleWork] Thread id - 2; inserted 1 row.<br>
[NsSampleWork] Thread id - 2 selected 1 row [923,Derby14        ,4122.9033,9180]<br>
[NsSampleWork] Thread id - 2; inserted 1 row.<br>
[NsSampleWork] Thread id - 2 selected 1 row [923,Derby14        ,4122.9033,9180]<br>
[NsSampleWork] Thread id - 2 selected 1 row [923,Derby14        ,4122.9033,9180]<br>
[NsSampleWork] Thread id - 2; closed connection to the database.<br>
[NsSampleClientThread] Thread id - 2; finished all tasks.<br>
[NsSample] Shutting down network server.<br>
Connection number: 4.<br>
Shutdown successful.<br>
[NsSample] End of Network server demo.<br>
<br>
</tt>

<P>Running the demo program will also create new directories and files:</P>
<UL>
  <LI><A name=Bot_Of_Page></A><tt>NSSampledb</tt><br>This directory makes up the 
NSSampledb database.</li>
  <LI><tt>derby.log</tt><br>This log file contains Derby progress and error messages.</li>
</ul>


</BODY>
</HTML>
